<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2018/5/30
 * Time: 下午1:47
 */

namespace App\Modules\Pay\Services;


use App\Modules\Pay\Models\PayWithdrawCash;

class PayWithdrawCashService
{

    /**
     * 保存提现信息
     * @param $uid 提现用户ID
     * @param $amount 提现金额
     * @param $remark 备注
     * @return mixed
     */
    public static function addWithdrawCash($uid, $amount, $remark) {
        $tallage = self::getWithdrawCashTallage($amount);

        $dataWithdrawCash['uid'] = $uid;
        $dataWithdrawCash['amount'] = $amount;
        $dataWithdrawCash['tallage'] = $tallage;
        $dataWithdrawCash['status'] = self::getWithdrawCashStatusStr(0);
        $dataWithdrawCash['remark'] = $remark;
        $dataWithdrawCash['payment_remark'] = '';
        //$dataWithdrawCash['serial_number'] = "";
        //$dataWithdrawCash['check_user_id'] = "";

        $withdrawCash = PayWithdrawCash::create($dataWithdrawCash);
        return $withdrawCash;
    }

    /**
     * 根据ID查找提现信息
     * @param $id
     * @return mixed
     */
    public static function getWithdrawCash($id) {
        return PayWithdrawCash::find($id);
    }


    /**
     * 提现列表
     * @param $params[]
     *                  uid:用户ID 可选
     *                  start_time:开始时间 可选
     *                  end_time:结束时间 可选
     * @param string $statusIn 状态 查看接口 WithdrawCashService::getWithdrawCashStatusList  可不传 0:待审核  10：待打款   20：打款中 30:打款成功 40 打款失败
     * @param $isAll
     * @return mixed
     */
    public static function getList($params=[], $statusIn = '', $isAll = false) {
        if(is_numeric($statusIn)) {
            $params['status'] = self::getWithdrawCashStatusStrList($statusIn);
        }
        return PayWithdrawCash::getList($params, $isAll);
    }



















    /**
     * 获取提现手续费
     * @param $amount
     * @return float
     */
    public static function getWithdrawCashTallage($amount) {
        //设置手续费比例
        return $amount * 0.05;
    }


    public static function getWithdrawCashStatusStr($statusIn = '') {
        $status = '';
        if(is_numeric($statusIn)) {
            if($statusIn == 0) {
                $status = "待审核";
            } else if($statusIn == 1) {
                $status = "审核中";
            } else if($statusIn == 2) {
                $status = "审核通过";
            } else if($statusIn == 9) {
                $status = "审核不通过";

            } else if($statusIn == 10) {
                $status = "待打款";
            } else if($statusIn == 11) {
                $status = "自动待打款";
            } else if($statusIn == 12) {
                $status = "人工待打款";

            } else if($statusIn == 20) {
                $status = "打款中";
            } else if($statusIn == 21) {
                $status = "自动打款中";
            } else if($statusIn == 22) {
                $status = "人工打款中";

            } else if($statusIn == 30) {
                $status = "打款成功";
            } else if($statusIn == 31) {
                $status = "自动打款成功";
            } else if($statusIn == 32) {
                $status = "人工打款成功";

            } else if($statusIn == 40) {
                $status = "打款失败";
            } else if($statusIn == 41) {
                $status = "自动打款失败";
            } else if($statusIn == 42) {
                $status = "人工打款失败";

            } else if($statusIn == -1) {
                $status = "其他错误";
            } else {
                $status = "";
            }
        }
        return $status;
    }

    public static function getWithdrawCashStatusStrList($statusIn = '') {
        $status = '';
        if(is_numeric($statusIn)) {
            $status = [];
            if($statusIn == 0) {
                $status[] = "待审核";
            } else if($statusIn == 1) {
                $status[] = "审核中";
            } else if($statusIn == 2) {
                $status[] = "审核通过";
            } else if($statusIn == 9) {
                $status[] = "审核不通过";

            } else if($statusIn == 10) {
                $status[] = "待打款";
                $status[] = "自动待打款";
                $status[] = "人工待打款";
            } else if($statusIn == 11) {
                $status[] = "自动待打款";
            } else if($statusIn == 12) {
                $status[] = "人工待打款";

            } else if($statusIn == 20) {
                $status[] = "打款中";
                $status[] = "自动打款中";
                $status[] = "人工打款中";
            } else if($statusIn == 21) {
                $status[] = "自动打款中";
            } else if($statusIn == 22) {
                $status[] = "人工打款中";

            } else if($statusIn == 30) {
                $status[] = "打款成功";
                $status[] = "自动打款成功";
                $status[] = "人工打款成功";
            } else if($statusIn == 31) {
                $status[] = "自动打款成功";
            } else if($statusIn == 32) {
                $status[] = "人工打款成功";

            } else if($statusIn == 40) {
                $status[] = "打款失败";
                $status[] = "自动打款失败";
                $status[] = "人工打款失败";
            } else if($statusIn == 41) {
                $status[] = "自动打款失败";
            } else if($statusIn == 42) {
                $status[] = "人工打款失败";

            } else if($statusIn == -1) {
                $status[] = "其他错误";
            } else {
                $status[] = "未知错误码";
            }
        }
        return $status;
    }


    public static function getWithdrawCashIntStatus($statusStr = '') {
        if(in_array($statusStr, ['待审核'])) {
            return 0;
        }
        if(in_array($statusStr, ['审核中'])) {
            return 1;
        }
        if(in_array($statusStr, ['审核通过'])) {
            return 2;
        }
        if(in_array($statusStr, ['审核不通过'])) {
            return 9;
        }
        if(in_array($statusStr, ['待打款', '自动待打款', '人工待打款'])) {
            return 10;
        }
        if(in_array($statusStr, ['打款中', '自动打款中', '人工打款中'])) {
            return 20;
        }
        if(in_array($statusStr, ['打款成功', '自动打款成功', '人工打款成功'])) {
            return 30;
        }
        if(in_array($statusStr, ['打款失败', '自动打款失败', '人工打款失败'])) {
            return 40;
        }
        if(in_array($statusStr, ['其他错误'])) {
            return -1;
        }
        return -1;
    }

}